package com.wanmao.test;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanmao.test.adapter.CommonAdapter;
import com.wanmao.test.bean.ProjectBean;
import com.wanmao.test.bean.RYRWBean;
import com.wanmao.test.bean.TaskInformationBean;
import com.wanmao.test.bean.TaskInformationBeanResultValue;
import com.wanmao.test.service.ProjectService;
import com.wanmao.test.service.RYRWService;
import com.wanmao.test.service.ToolShowDemo;
import com.wanmao.test.util.DateUtils;
import com.wanmao.test.util.DialogUtils;
import com.wanmao.test.util.StaticUtil;
import com.wanmao.test.view.XListView;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class ProjectListActivity extends BaseActivity implements View.OnClickListener, XListView.IXListViewListener{
    private XListView lv_test;

    private MyAdapter adapter;
    private boolean refreshFlag = false;
    private boolean loadmoreFlag = false;
    private int pageIndex = 1;
    private int pageSize = 10;
    private List<ProjectBean> list;
    private List<RYRWBean> listRW;
    private ProjectService service=new ProjectService(this);
    private RYRWService ryrwService =new RYRWService(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page01);
        listRW = ryrwService.findALLRW(StaticUtil.ISC_ID);
        list=service.findAllByList(listRW);
        intenView();
    }



    private void intenView() {
        adapter = new MyAdapter(this, list);
        lv_test = (XListView) findViewById(R.id.lv_test);
        lv_test.setEmptyView(findViewById(R.id.tv_empty));
        lv_test.setXListViewListener(this);
        lv_test.setPullLoadEnable(false);
        lv_test.setPullRefreshEnable(false);
        lv_test.setAdapter(adapter);

        lv_test.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int person=i-1;

                String title=list.get(person).getRWMC();
                String JDJD = list.get(person).getJDJD();

                String ZYLX=list.get(person).getZYLX();
                String HDNR=list.get(person).getHDNR();
                String time =list.get(person).getHDSJ();
                String RwID =list.get(person).getRWID();
                String DZHXL =list.get(person).getDZHXL();
                String DZHXLID=list.get(person).getDZHXLID();
                Intent intent = new Intent();
                intent.putExtra("RWID", RwID);
                intent.putExtra("time", time);
                intent.putExtra("title", title);
                intent.putExtra("gzjd", JDJD);
                intent.putExtra("ZYLX", ZYLX);
                intent.putExtra("HDNR", HDNR);
                intent.putExtra("DZHXL", DZHXL);
                intent.putExtra("DZHXLID", DZHXLID);

                intent.setClass(ProjectListActivity.this, Test_ProjectDetailsActivity.class);
                startActivity(intent);
            }
        });


    }

    @Override
    public void onPullDownRefresh() {
        DialogUtils.startProgressDialog(this, "正在加载");
        refreshFlag = true;
        pageIndex = 1;

    }

    @Override
    public void onLoadMore() {
        loadmoreFlag = true;

        pageIndex++;

    }

    @Override
    public void onClick(View view) {

    }

    class MyAdapter extends CommonAdapter<ProjectBean> {


        public MyAdapter(Context context, List<ProjectBean> mDatas) {
            super(context, mDatas);

        }

        public void setData(List<ProjectBean> datas) {
            mDatas = datas;
            notifyDataSetChanged();
        }

        public void addData(List<ProjectBean> datas) {
            mDatas.addAll(datas);
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_test, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_item_test_title);
                viewHolder.tv_gzjd = (TextView) convertView.findViewById(R.id.tv_item_test_gzjd);
                viewHolder.tv_zylx = (TextView) convertView.findViewById(R.id.tv_item_test_zylx);
                viewHolder.tv_hdsj = (TextView) convertView.findViewById(R.id.tv_item_test_hdsj);
                viewHolder.tv_rwid = (TextView) convertView.findViewById(R.id.tv_item_test_rwid);
                viewHolder.btn_do = (Button) convertView.findViewById(R.id.btn_item_test_do);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final String title = mDatas.get(position).getRWMC();
            viewHolder.tv_title.setText(title);
            final String GZJD = mDatas.get(position).getJDJD();

            viewHolder.tv_gzjd.setText(GZJD);
            final String OBJ_ID = mDatas.get(position).getRWID();
            viewHolder.tv_rwid.setText(OBJ_ID);

            viewHolder.tv_zylx.setText(mDatas.get(position).getZYLX());
            String time = mDatas.get(position).getHDSJ();
            viewHolder.tv_hdsj.setText(time);

            return convertView;
        }
    }


    public static class ViewHolder {
        public TextView tv_title;
        public TextView tv_gzjd;
        public TextView tv_zylx;
        public TextView tv_hdsj;
        public TextView tv_rwid;
        public Button btn_do;
    }
}
